/**
 * @author KujoMiva
 * @name config.default.js
 * @desc 项目默认配置
 * @time 2020年05月12日 17:40:27 星期二
 */
"use strict";

const AK = require("../AccessKey");
const path = require("path");
const fs = require("fs");
/**
 * @param {Egg.EggAppInfo} appInfo app info
 */
module.exports = appInfo => {
    /**
    * built-in config
    * @type {Egg.EggAppConfig}
    **/
    const config = exports = {};

    // use for cookie sign key, should change to your own and keep security
    config.keys = `${appInfo.name}_QQ_748943649`;

    // add your middleware config here
    config.middleware = [];

    // 日志输出设置
    config.logger = {
        outputJSON: true,
    };

    // 文件上传配置
    config.multipart = {
        mode: "file",
    };

    // csfr token 字段配置
    config.security = {
        csrf: false,
    };

    // 跨域配置
    config.cors = {
        origin: "*",
        allowMethods: "GET,HEAD,PUT,POST,DELETE,PATCH,OPTIONS",
    };

    // egg-jwt 配置
    config.jwt = {
        enable: false,
        secret: AK.JWT.secret,
    };

    // 开启前置代理模式
    config.proxy = true;
    config.ipHeaders = "X-Real-Ip, X-Forwarded-For";

    // 静态资源配置
    config.static = {
        prefix: "/apidoc/",
        dir: path.join(appInfo.baseDir, "doc/"),
        dynamic: true,
        preload: false,
    };

    // egg-validate-plus参数验证配置
    config.validatePlus = {
        /** @param {Egg.Context} ctx - egg context */
        resolveError: ctx => ctx.returnBody(10086, ""),
    };

    // 以读取本地文件的方式修改图标
    config.siteFile = {
        "/favicon.ico": fs.readFileSync(path.join(__dirname, "../favicon.ico")),
    };

    // sequelize连接配置
    config.sequelize = {
        dialect: "mysql",
        database: "jky_mysql_common",
        host: AK.MySql.host,
        port: AK.MySql.port,
        username: Buffer.from(AK.MySql.username, "base64").toString("ascii"),
        password: Buffer.from(AK.MySql.password, "base64").toString("ascii"),
        // 连接池设置
        pool: {
            max: 50,
            min: 1,
        },
        define: {
            timestamps: true, // 默认开启时间戳
            paranoid: true, // 软删除配置
            freezeTableName: true, // 冻结表名
            underscored: true,
            createdAt: "created_at", // 自定义时间戳
            updatedAt: "updated_at", // 自定义时间戳
            deletedAt: "deleted_at", // 自定义时间戳
        },
        timezone: "+08:00", // 服务器时区
    };

    // add your user config here
    const userConfig = {
        serverHost: "server.miva.work", // 服务器域名
        ossHost: "resource.miva.work", // 云存储域名
        ossBucket: "miva-work-resource", // 云存储桶名
        ossEndpoint: "resource.miva.work", // 云存储终端
        ossRegion: "oss-cn-beijing", // 云存储地址
    };

    return {
        ...AK,
        ...config,
        ...userConfig,
    };
};
